The code to create an empty file in PHP [file_put_contents vs touch]

  • 2020-05-10 17:54:16
  • OfStack

I has passed a small test to check which function is faster to create a new file.

file_put_contents vs touch
 
<?php 
for($i = ; $i < 100; $i++) 
{ 
file_put_contents('dir/file'.$i, ''); 
} 
?> 

Average time: 0,1145s
 
<?php 
for($i = ; $i < 100; $i++) 
{ 
touch('dir/file'.$i); 
} 
?> 

Average time: 0,2322s

So, file_put_contents is about twice as fast as touch.

Related articles: